package ics314.ui.panel;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Random;

import ics314.RadioStation;
import ics314.Vor;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class CompoundVor extends JPanel {
	
	private Vor v;
	private CompoundVorListener l;
	private Random rng;

	private SimpleVor vorPanel;
	private JPanel buttonPanel;
	private LightLabelPanel stationID;
	private JPanel radPanel;
	private JPanel obsPanel;
	private JPanel sigPanel;
	
	private JSpinner radSelect;
	private JButton radConfirm;
	private JButton radRandom;
	
	private JSpinner obsSelect;
	private JButton obsConfirm;
	private JButton obsRandom;
	
	private JButton sigGood;
	private JButton sigBad;
	private JButton sigRandom;
	
	
	
	public CompoundVor(Vor v) throws IOException {
		
		super();
		BoxLayout b = new BoxLayout(this, BoxLayout.X_AXIS);
		this.setLayout(b);
		this.v = v;
		l = new CompoundVorListener();
		rng = new Random();
		
		vorPanel = new SimpleVor(v);
		buttonPanel = new JPanel(new GridLayout(5, 1));
		stationID = new LightLabelPanel(v);
		
		initializeSelectorPanel();
		buttonPanel.add(new JPanel());
		
		
		this.add(vorPanel);
		this.add(buttonPanel);
		
		buttonPanel.add(stationID);
		
	}
	
	private void initializeSelectorPanel() {
		
		radPanel = new JPanel(new FlowLayout());
		obsPanel = new JPanel(new FlowLayout());
		sigPanel = new JPanel(new GridLayout(1, 3));
		
		radSelect = new JSpinner(new SpinnerNumberModel(v.getRadial(), 0, 359, 1));
		radConfirm = new JButton("S");
		radRandom = new JButton("R");
		
		radConfirm.addActionListener(l);
		radRandom.addActionListener(l);
		
		obsSelect = new JSpinner(new SpinnerNumberModel(v.getObs(), 0, 359, 1));
		obsConfirm = new JButton("S");
		obsRandom = new JButton("R");
		
		obsConfirm.addActionListener(l);
		obsRandom.addActionListener(l);
		
		sigGood = new JButton("G");
		sigBad = new JButton("B");
		sigRandom = new JButton("R");
		
		sigGood.addActionListener(l);
		sigBad.addActionListener(l);
		sigRandom.addActionListener(l);
		
		radPanel.add(radSelect);
		radPanel.add(radConfirm);
		radPanel.add(radRandom);
		
		obsPanel.add(obsSelect);
		obsPanel.add(obsConfirm);
		obsPanel.add(obsRandom);
		
		sigPanel.add(sigGood);
		sigPanel.add(sigBad);
		sigPanel.add(sigRandom);
		
		radPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Radial"));
		obsPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "OBS"));
		sigPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Signal Quality"));
		
		buttonPanel.add(radPanel);
		buttonPanel.add(obsPanel);
		buttonPanel.add(sigPanel);
		
		buttonPanel.setMaximumSize(new Dimension(200, 300));
	}
	
	private class CompoundVorListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			if(e.getSource() == obsConfirm) {
				v.setObs((Integer) obsSelect.getValue()); 
			}
			else if(e.getSource() == obsRandom) {
				int obs = rng.nextInt(360);
				v.setObs(obs);
				obsSelect.setValue(obs);
			}
			else if(e.getSource() == radConfirm) {
				v.setRadial((Integer) radSelect.getValue()); 
			}
			else if(e.getSource() == radRandom) {
				int radial = rng.nextInt(360);
				v.setRadial(radial);
				radSelect.setValue(radial);
			}
			else if(e.getSource() == sigGood) {
				v.setRadioStation(new RadioStation("GDS", v.getRadial(), false, false, null));
			}
			else if(e.getSource() == sigBad) {
				v.setRadioStation(new RadioStation("BAD", v.getRadial(), true, true, null));
			}
			else if(e.getSource() == sigRandom) {
				boolean over = rng.nextInt(4) == 0;
				boolean far = rng.nextInt(4) == 0;
				v.setRadioStation(new RadioStation("RND", v.getRadial(), far, over, null));
			}
			
			vorPanel.setVisible(false);
			vorPanel.repaint();
			vorPanel.setVisible(true);
			stationID.update();
			
		}
		
		
		
	}
	
	private static class LightLabelPanel extends JPanel{
		
		private Vor v;
		
		private LightLabelPanel(Vor v) {
			this.v = v;
		}
		
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2 = (Graphics2D) g;
			
			g2.drawString("Reading from station: " + v.getRadioStationID(), 0, 58);
		}
		
		public void update() {
			this.setVisible(false);
			this.repaint();
			this.setVisible(true);
		}
		
	}
}
